﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Longbow.UI");

Longbow.UI.FireEnterKeyBehavior = function(element) {
    Longbow.UI.FireEnterKeyBehavior.initializeBase(this, [element]);
    this._keyPressHandler = null;
    this._fireButton = null;
    this._fireButtonHandler = null;
}

Longbow.UI.FireEnterKeyBehavior.prototype = {
    initialize: function() {
        Longbow.UI.FireEnterKeyBehavior.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var element = this.get_element();
        this._keyPressHandler = Function.createDelegate(this, this._keyPress);
        $addHandler(element, "keypress", this._keyPressHandler);
    },
    
    _keyPress: function(eventObject) {
        if (eventObject.charCode === 13) {
            eventObject.preventDefault();
            eventObject.stopPropagation();            
            var btn = this.get_fireButton();
            window.setInterval(function() {btn.click();}, 0);
        }
    },
    
    set_fireButton: function(value) {
        /// <summary />
        /// <param name="value" type="HTMLElement" />
        var e = Function._validateParams(arguments, [
            {name: "value", domElement: true}
        ]);
        if (e) throw e;        
        this._fireButton = value;
        
        this._fireButtonHandler = Function.createDelegate(this, this._fireButtonClick);
        $addHandler(this._fireButton, "click", this._fireButtonHandler);
    },
    
    get_fireButton: function() {
        return this._fireButton;
    },
    
    _fireButtonClick: function(eventObject) {
        var element = this.get_element();
        var behavior = Sys.UI.Behavior.getBehaviorByName(element, "Watermark");
        element.value = behavior.get_text();
    },
    
    dispose: function() {        
        // Add custom dispose actions here
        var element = this.get_element();
        if (this._keyPressHandler !== null) {
            $removeHandler(element, "keypress", this._keyPressHandler);
        }
        this._keyPressHandler = null;
        if (this._fireButtonHandler !== null) {
            $removeHandler(this.get_fireButton(), "click", this._fireButtonHandler);
        }
        this._fireButtonHandler = null;

        Longbow.UI.FireEnterKeyBehavior.callBaseMethod(this, 'dispose');
    }
}
Longbow.UI.FireEnterKeyBehavior.registerClass('Longbow.UI.FireEnterKeyBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
